package com.servlet;

import com.bean.Repair;
import com.google.gson.Gson;
import com.service.impl.RepairServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/RepairServlet/*")
public class RepairServlet extends HttpServlet {

    RepairServiceImpl repairService = new RepairServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html,charset=utf-8");//命令浏览器用什么编码打开文件

        String pathInfo = request.getPathInfo().substring(1);
        if("listAll".equals(pathInfo)){
            listAll(request,response);
        }else if("listByName".equals(pathInfo)){
            listByName(request,response);
        }else if("deleteById".equals(pathInfo)){
            deleteById(request,response);
        }else if("removeAll".equals(pathInfo)){
            removeAll(request,response);
        }else if("listAllByPagination".equals(pathInfo)){
            listAllByPagination(request,response);
        }else if("repairAdd".equals(pathInfo)){
           repairAdd(request,response);
        }else if("updateById".equals(pathInfo)){
            updateById(request,response);
        }else if("listAllByPaginationSearch".equals(pathInfo)){
            listAllByPaginationSearch(request,response);
        }
    }

    private void listByName(HttpServletRequest request, HttpServletResponse response) {
        String name  = request.getParameter("name");
        System.out.println(name);
        try {
            List<Repair> rList = repairService.selectByName(name);
            System.out.println(rList);
            Gson gson = new Gson();
            String s =gson.toJson(rList);
            System.out.println(s);
            response.getWriter().print(s);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void listAllByPaginationSearch(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));
//        String search = request.getParameter("search");
        String filter  = request.getParameter("filter");
        Gson gson = new Gson();
        Map<String,String> map1 = gson.fromJson(filter,Map.class);

        try {
            List<Repair> repairList = repairService.selectAllPaginationSearch(offset,limit,map1);
            int total = repairService.countSearch(map1);
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",repairList);

            String s =gson.toJson(map);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   private void updateById(HttpServletRequest request, HttpServletResponse response) {
        Integer repairId = Integer.valueOf(request.getParameter("repairId"));
        String ownerId = request.getParameter("ownerId");
        Integer repairPhone = Integer.valueOf(request.getParameter("repairPhone"));
        String repairAddr = request.getParameter("repairAddr");
        String repairState = request.getParameter("repairState");
        String repairCreatetime = request.getParameter("repairCreatetime");
        String repairEndtime = request.getParameter("repairEndtime");
        String repairContents = request.getParameter("repairContents");
        String repairPleased = request.getParameter("repairPleased");
        String serviceAssess = request.getParameter("serviceAssess");
        Integer employeeId = Integer.valueOf(request.getParameter("employeeId"));

        Repair repair = new Repair(repairId,ownerId,repairPhone,repairAddr,repairState,repairCreatetime,repairEndtime,repairContents,repairPleased,serviceAssess,employeeId);
        try {
            boolean b = repairService.update(repair);
            if(b){
                response.getWriter().print("success");
            }else{
                response.getWriter().print("");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void repairAdd(HttpServletRequest request, HttpServletResponse response) {

        String ownerId = request.getParameter("ownerId");
        Integer repairPhone = Integer.valueOf(request.getParameter("repairPhone"));
        String repairAddr = request.getParameter("repairAddr");
        String repairCreatetime = request.getParameter("repairCreatetime");
        String time = repairCreatetime.replace("T", " ");
        String repairContents = request.getParameter("repairContents");
        Repair repair = new Repair(null,ownerId,repairPhone,repairAddr,"未修",time,"暂时未知",repairContents,"尚未评价","尚未评价",Integer.valueOf("0"));
        try {
            boolean b = repairService.insert(repair);
            if(b){
                request.setAttribute("msg","新增成功");
            }else{
                request.setAttribute("msg","新增失败");
            }
            request.getRequestDispatcher("/property/repair_add.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAllByPagination(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));

        try {
            List<Repair> ownerList = repairService.selectAllPagination(offset,limit);
            int total = repairService.count();
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",ownerList);

            Gson gson = new Gson();
            String s =gson.toJson(map);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void removeAll(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        String[] idsArr = ids.split(",");
        try {
            boolean b = repairService.removeAll(idsArr);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) {
        Integer repairId = Integer.valueOf(request.getParameter("repairId"));
        try {
            boolean b = repairService.deleteById(repairId);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAll(HttpServletRequest request, HttpServletResponse response) {

        try {
            List<Repair> ownerList = repairService.selectAll();

            Gson gson = new Gson();
            String s = gson.toJson(ownerList);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
